import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import Layout from "../layout/index.vue";
import { getAuthRouters } from "@/router/authRouter";
import { getDynamicRoutes } from "@/utils/getData.js";
import { useAuthRouterStore } from "@/stores/authRouter.js";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/admin",
      name: "首页",
      component: Layout,
      redirect: "/dashboard",
      meta: {
        staticRouter: true, // 静态路由
      },
      children: [
        {
          path: "dashboard",
          name: "dashboard",
          component: () => import("@/views/Dashboard/index.vue"),
          meta: {
            breadcrumbName: "首页",
            icon: "dashboard",
            staticRouter: true, // 静态路由
          },
        },
      ],
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/Login/index.vue"),
    },
    {
      path: "/",
      name: "index",
      component: () => import("../views/index.vue"),
    },

    {
      path: "/analysis",
      name: "analysis",
      component: () => import("../views/analysis.vue"),
    },
    {
      path: "/orderQuery",
      name: "orderQuery",
      component: () => import("../views/orderQuery/index.vue"),
    },
    {
      path: "/orderPay",
      name: "orderPay",
      component: () => import("../views/orderPay/index.vue"),
    },
    {
      path: "/paySuccess",
      name: "paySuccess",
      component: () => import("../views/paySuccess/index.vue"),
    },
    {
      path: "/analysisReport",
      name: "analysisReport",
      component: () => import("../views/analysisReport/index.vue"),
    },
    {
      path: "/afterPay",
      name: "afterPay",
      component: () => import("../views/afterPay/index.vue"),
    },
    {
      path: "/makesure",
      name: "makesure",
      component: () => import("../views/makesure/index.vue"),
    },
    {
      path: "/BackgroundSystem",
      name: "BackgroundSystem",
      component: () => import("../views/BackgroundSystem/index.vue"),
    },
    {
      path: "/BirthFrom",
      name: "BirthFrom",
      component: () => import("../views/BirthFrom/index.vue"),
    },
  ],
});

// 验证 token
router.beforeEach(async (to, from, next) => {
  const token = sessionStorage.token;

  // 定义无需登录即可访问的路由白名单
  const whiteList = ["/index", "/guoxue", "/about", "/", "/analysis"]; // 添加你的公共路由路径
  next();
  return;
  // 如果当前路径在白名单中，直接放行
  if (whiteList.includes(to.path)) {
    next();
  }
  if (to.path !== "/login" && !token) {
    next("/login");
  } else {
    const authRouterStore = useAuthRouterStore();
    let { routerList } = authRouterStore;
    if (!routerList.length) {
      let dynamicRoutes = await getDynamicRoutes();
      // 动态添加路由
      let newRoutes = getAuthRouters(dynamicRoutes);
      authRouterStore.addRouterList(newRoutes);
      newRoutes.forEach((val) => {
        router.addRoute(val);
      });
      next({ path: to.path });
    } else {
      next();
    }
  }
});
export default router;
